<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/index' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/index/articleList' }"
        >>文章管理</el-breadcrumb-item
      >
      <el-breadcrumb-item>文章列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <el-table
        height="calc( 100vh - 230px )"
        :data="tableData"
        border
        style="width: 100%"
      >
        <el-table-column type="index" label="" width="60"> </el-table-column>
        <el-table-column prop="title" label="标题"> </el-table-column>
        <el-table-column
          :formatter="classifyFn"
          prop="type"
          label="类型"
          width="100"
        >
        </el-table-column>
        <el-table-column prop="user.nickname" label="作者" width="100">
        </el-table-column>
        <el-table-column prop="address" label="操作" width="100">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[5, 10, 15]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import { articleList } from "@/API/news";
export default {
  data() {
    return {
      tableData: [],
      total: 0,
      currentPage: 1,
      pageSize: 10000,
    };
  },
  created() {
    this.getArticleList();
  },
  methods: {
    // 编辑按钮
    handleEdit(index, row) {
      this.$router.push({
        path: "/index/addArticle",
      });
      console.log(index, row);
    },
    // 删除
    handleDelete(index, row) {
      console.log(index, row);
    },
    // 文章类别判定
    classifyFn(row, column, cellValue, index) {
      // console.log(74, row, column, cellValue, index);
      if (cellValue == 1) {
        return "文章";
      } else {
        return "视频";
      }
    },
    // 每页显示数量
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.getArticleList();
    },
    // 切换页数
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.currentPage = val;
      this.getArticleList();
    },
    // 文章列表函数封装
    getArticleList() {
      articleList({
        pageIndex: this.currentPage,
        pageSize: this.pageSize,
      }).then((res) => {
        // console.log(res);
        this.tableData = res.data.data;
        if (!res.data.total) {
          this.total = 10000;
        } else {
          this.total = res.data.total;
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.el-breadcrumb {
  margin-bottom: 15px;
}
.block {
  margin-top: 15px;
  padding-left: 10px;
}
</style>